//
//  KRComplicationVC.m
//  QuickCare
//
//  Created by 张凯锐 on 16/7/13.
//  Copyright © 2016年 lidabiao. All rights reserved.
//

#import "KRComplicationVC.h"
#import "KRComplication.h"

@interface KRComplicationVC ()<UITableViewDelegate,UITableViewDataSource>
//并发症数组
@property (nonatomic,strong) NSMutableArray *complicationArr;
//统计数
@property (nonatomic,assign) int index;
//并发症
@property (nonatomic,strong) NSMutableString *name;
@end

@implementation KRComplicationVC
- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"确定" style:UIBarButtonItemStylePlain target:self action:@selector(clickDetermine)];
    [SVProgressHUD showWithStatus:@"加载中..."];
    [self requestData];
}

//点击确定的方法
- (void)clickDetermine {
    //将数据给一个字符串
    NSString *str = [NSString stringWithFormat:@"[%@][共选择%d个]",self.name,self.index];
    //找出共选择的位置
    NSRange range = [str rangeOfString:[NSString stringWithFormat:@"[共选择%d个]",self.index]];
    //创建富文本
    NSMutableAttributedString *attributedStr = [[NSMutableAttributedString alloc] initWithString:str];
    //给富文本一段字换颜色
    [attributedStr addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithRed:54/255.f green:190/255.f blue:189/255.f alpha:0.6] range:range];
    
    if ([self.delegate respondsToSelector:@selector(complicationVCDelegate:complicationName:)]) {
        [self.delegate complicationVCDelegate:self complicationName:attributedStr];
    }
    
    [self.navigationController popViewControllerAnimated:YES];
}

//请求数据
- (void)requestData {
    //url
    NSString *url = @"http://iosapi.itcast.cn/doctor/complicationList.json.php";
    //请求参数
    NSDictionary *parameters = @{@"page":@1,@"page_size":@15,@"ci1_id":@(self.ci1_id+1),@"ci2_id":@(self.ci2_id)};
    [[NetworkManager shareManager] requestDataWithURLString:url parameters:parameters success:^(NSDictionary * response) {
        //用YYModel字典转模型
        NSArray *data = [NSArray yy_modelArrayWithClass:[KRComplication class] json:response[@"data"]];
        [self.complicationArr addObjectsFromArray:data];
        if (self.complicationArr.count == 0) {
            [SVProgressHUD showErrorWithStatus:@"该疾病类型没有并发症"];
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [self.navigationController popViewControllerAnimated:YES];
            });
        } else {
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [SVProgressHUD dismiss];
                //取完数据刷新tableView
                [self.tableView reloadData];
            });
        }
    } failure:^(NSError *error) {
        NSLog(@"%@",error);
    }];
}

#pragma mark - UITableViewDataSource数据源
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.complicationArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    }
    
    //数据数组
    KRComplication *complication = self.complicationArr[indexPath.row];
    //给cell赋值
    cell.textLabel.text = complication.complication_name;
    return cell;
}

#pragma mark - UITableViewDelegate代理
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    
    //如果cell的accessoryType已经是Checkmark类型,则设置为None,反之.
    if (cell.accessoryType == UITableViewCellAccessoryCheckmark) {
        cell.accessoryType = UITableViewCellAccessoryNone;
        self.index -= 1;
        //找出cell的文字位置
        NSRange range = [self.name rangeOfString:cell.textLabel.text];
        //将这个cell的文字删除
        [self.name deleteCharactersInRange:range];
    } else {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
        self.index += 1;
        //添加cell的文字进去
        [self.name appendString:[NSString stringWithFormat:@"%@,",cell.textLabel.text]];
    }
}

//懒加载
- (NSMutableArray *)complicationArr {
    if (_complicationArr == nil) {
        _complicationArr = [NSMutableArray array];
    }
    return _complicationArr;
}

- (NSMutableString *)name {
    if (_name == nil) {
        _name = [NSMutableString string];
    }
    return _name;
}

@end
